<?php

use App\Models\Admin\Permission;
use App\Models\Admin\Role;
use App\Models\Admin\User;
use Illuminate\Database\Seeder;
use DB;

class PermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //取消外键约束
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        //清空相关的数据表
        Permission::truncate();
        Role::truncate();
        User::truncate();
        DB::table('role_user')->delete();
        DB::table('permission_role')->delete();
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');

        //创建初始数据
        $admin= User::create([
            'name' => 'admin',
            'email' => 'admin@qq.com',
            'password' => bcrypt('123qwe')
        ]);
        $role = Role::create([
            'name' => 'admin',
            'display_name' => '管理员',
            'description' => 'super admin role'
        ]);
        $permissions = [
            [
                'name' => 'create_user',
                'display_name' => '创建用户',
                'description' => '管理用户的权限'
            ],[
                'name' => 'edit_user',
                'display_name' => '编辑用户',
                'description' => '管理用户的权限'
            ],[
                'name' => 'delete_user',
                'display_name' => '删除用户',
                'description' => '管理用户的权限'
            ],
        ];
        foreach($permissions as $p){
            $permission = Permission::create($p);
            //给角色赋予响应的权限
            $role->attachPermission($permission);
        }

        //给用户赋予响应的角色
        $admin->attachRole($role);
    }
}
